package com.wisemoresystem.seal.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.ne.framework.data.util.ChkTools;

public class DateTools {

	// 增加对应天数
	public static Timestamp addDay(Timestamp end, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(end.getTime());

		cal.add(Calendar.DAY_OF_YEAR, day);
		return new Timestamp(cal.getTimeInMillis());

	}

	public static int countDays(Timestamp begin, Timestamp end) {
		long beginTime = begin.getTime();
		long endTime = end.getTime();
		int days = (int) ((endTime - beginTime) / (1000 * 60 * 60 * 24));
		return days;
	}

	public static void main(String[] args) {
		Timestamp t = new Timestamp(System.currentTimeMillis());
		System.err.println(t);
		t = DateTools.addDay(t, 5);
		System.err.println(t);
	}

	/**
	 * 字符串转 Timestamp对象
	 *
	 * @param date
	 */
	public static Timestamp strToTimestamp(String date) {
		String pattern = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			Date d = sdf.parse(date);
			return new Timestamp(d.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String addOneDay(String date) {
		String pattern = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			Date d = sdf.parse(date);
			Timestamp time = new Timestamp(d.getTime());
			return DateTools.addDay(time, 1).toString();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 字符串转 Timestamp对象
	 *
	 * @param date
	 */
	public static Timestamp strToDatestamp(String date) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			Date d = sdf.parse(date);
			return new Timestamp(d.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 字符串转 Timestamp对象
	 *
	 * @param date
	 */
	public static java.sql.Date strToDate(String date) {
		String pattern = "yyyy-MM-dd";
		return strToDate(date, pattern);
	}

	public static java.sql.Date strToDate(String date, String pattern) {
		if (ChkTools.isNull(date)) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			Date d = sdf.parse(date);
			return new java.sql.Date(d.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 取得 2014-10 这样的字符串
	public static String getYearMonth() {
		String pattern = "yyyy-MM";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date(System.currentTimeMillis()));
	}

	public static java.sql.Date getYearMonth(String date) {
		String pattern = "yyyy-MM";
		return strToDate(date, pattern);
	}

	public static int getYear() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		return year;
	}

	public static int getMonth() {
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH);
		return month;
	}

	public static String getToday() {
		Calendar cal = Calendar.getInstance();
		java.util.Date date = cal.getTime();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	public static String getTimestampTodayStr() {
		Calendar cal = Calendar.getInstance();
		java.util.Date date = cal.getTime();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	public static String getLastYearMonth() {
		int year = getYear();
		int month = getMonth();
		if (month == 0) {
			month = 12;
			year--;
		}
		if (month < 10) {
			return year + "0" + month;
		}
		return year + "" + month;
	}

	public static String getYesterday() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd ").format(cal
				.getTime());
		return yesterday;
	}

	// 获取本周的第一天
	public static String getMonday() {
		Calendar cal = Calendar.getInstance();
		int d = cal.get(Calendar.DAY_OF_WEEK) -1;
		if(d>0){
			cal.add(Calendar.DATE, d-1);
		}else{
			cal.add(Calendar.DATE, -6);
		}
		
		String day = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
		return day;

	}
}
